package com.mbyte.easy.support.es.template;

import com.mbyte.easy.support.es.util.PageList;
import com.mbyte.easy.support.es.util.PageSortHighLight;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.reindex.BulkByScrollResponse;

import java.util.List;

/**
 * @Author:lxt
 * @Description:to do
 * @Date:2019/10/9
 * @Version:1.0
 */
public interface ElasticsearchTemplate<T,M> {
    /**
     * @Title: createIndex
     * @Description: 创建索引
     * @Author: lxt 
     * @param: clazz
     * @Date: 2019-10-26 17:38 
     * @throws: 
     */
    void createIndex(Class clazz)throws Exception;

    /**
     * 新增索引
     * @param t
     */
    boolean save(T t) throws Exception;


    /**
     * 新增索引集合
     * @param list
     */
    BulkResponse save(List<T> list) throws Exception;


    /**
     * 更新索引集合
     * @param list
     * @return
     * @throws Exception
     */
    BulkResponse bulkUpdate(List<T> list) throws Exception;


    /**
     * 按照有值字段更新索引
     * @param t
     */
    boolean update(T t) throws Exception;

    /**
     * 根据queryBuilder所查结果，按照有值字段更新索引
     *
     * @param queryBuilder
     * @param t
     * @param clazz
     * @param limitcount 更新字段不能超出limitcount
     * @param asyn true异步处理  否则同步处理
     * @return
     * @throws Exception
     */
    BulkResponse batchUpdate(QueryBuilder queryBuilder, T t, Class clazz, int limitcount, boolean asyn) throws Exception;

    /**
     * 覆盖更新索引
     * @param t
     */
    boolean updateCover(T t) throws Exception;

    /**
     * 删除索引
     * @param t
     */
    boolean delete(T t) throws Exception;

    /**
     * 根据条件删除索引
     * https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-high-document-delete-by-query.html#java-rest-high-document-delete-by-query-response
     * @param queryBuilder
     * @param clazz
     * @return
     * @throws Exception
     */
    BulkByScrollResponse deleteByCondition(QueryBuilder queryBuilder, Class<T> clazz) throws Exception;

    /**
     * 删除索引
     * @param id
     */
    boolean deleteById(M id, Class<T> clazz) throws Exception;


    /**
     * 【最原始】查询
     * @param searchRequest
     * @return
     * @throws Exception
     */
    SearchResponse search(SearchRequest searchRequest) throws Exception;

    /**
     * 非分页查询
     * 目前暂时传入类类型
     * @param queryBuilder
     * @param clazz
     * @return
     * @throws Exception
     */
    List<T> search(QueryBuilder queryBuilder, Class<T> clazz) throws Exception;

    /**
     * 非分页查询(跨索引)
     * 目前暂时传入类类型
     * @param queryBuilder
     * @param clazz
     * @return
     * @throws Exception
     */
    List<T> search(QueryBuilder queryBuilder, Class<T> clazz, String... indexs) throws Exception;

    List<T> searchWithHitScore(QueryBuilder queryBuilder, Class<T> clazz) throws Exception;

    /**
     * @Title: searchOne
     * @Description:
     * @Author: lxt
     * @param: queryBuilder
     * @param: clazz
     * @Date: 2019-10-18 19:03
     * @return: T
     * @throws:
     */
    T searchOne(QueryBuilder queryBuilder, Class<T> clazz) throws Exception;



    /**
     * 非分页查询，指定最大返回条数
     * 目前暂时传入类类型
     * @param queryBuilder
     * @param limitSize 最大返回条数
     * @param clazz
     * @return
     * @throws Exception
     */
    List<T> searchMore(QueryBuilder queryBuilder, int limitSize, Class<T> clazz) throws Exception;

    /**
     * 非分页查询(跨索引)，指定最大返回条数
     * 目前暂时传入类类型
     * @param queryBuilder
     * @param limitSize 最大返回条数
     * @param clazz
     * @return
     * @throws Exception
     */
    List<T> searchMore(QueryBuilder queryBuilder, int limitSize, Class<T> clazz, String... indexs) throws Exception;



    /**
     * 查询数量
     * @param queryBuilder
     * @param clazz
     * @return
     * @throws Exception
     */
    long count(QueryBuilder queryBuilder, Class<T> clazz) throws Exception;


    /**
     * 查询数量(跨索引)
     * @param queryBuilder
     * @param clazz
     * @return
     * @throws Exception
     */
    long count(QueryBuilder queryBuilder, Class<T> clazz, String... indexs) throws Exception;

    /**
     * 支持分页、高亮、排序的查询
     * @param queryBuilder
     * @param pageSortHighLight
     * @param clazz
     * @return
     * @throws Exception
     */
    PageList<T> search(QueryBuilder queryBuilder, PageSortHighLight pageSortHighLight, Class<T> clazz) throws Exception;


    /**
     * 支持分页、高亮、排序的查询（跨索引）
     * @param queryBuilder
     * @param pageSortHighLight
     * @param clazz
     * @return
     * @throws Exception
     */
    PageList<T> search(QueryBuilder queryBuilder, PageSortHighLight pageSortHighLight, Class<T> clazz, String... indexs) throws Exception;



    /**
     * scroll方式查询(默认了保留时间为Constant.DEFAULT_SCROLL_TIME)
     * @param queryBuilder
     * @param clazz
     * @return
     * @throws Exception
     */
    List<T> scroll(QueryBuilder queryBuilder, Class<T> clazz) throws Exception;

    /**
     * scroll方式查询
     * @param queryBuilder
     * @param clazz
     * @param time
     * @param indexs
     * @return
     * @throws Exception
     */
    List<T> scroll(QueryBuilder queryBuilder, Class<T> clazz, long time, String... indexs) throws Exception;

    /**
     * 根据ID查询
     * @param id
     * @param clazz
     * @return
     * @throws Exception
     */
    T getById(M id, Class<T> clazz) throws Exception;

    /**
     * 根据ID列表批量查询
     * @param ids
     * @param clazz
     * @return
     * @throws Exception
     */
    List<T> mgetById(M[] ids, Class<T> clazz) throws Exception;

    /**
     * id数据是否存在
     * @param id
     * @param clazz
     * @return
     */
    boolean exists(M id, Class<T> clazz) throws Exception;
}
